//
//  STStructViewController.m
//  STNewTools
//
//  Created by stoneobs on 17/4/21.
//  Copyright © 2017年 stoneobs. All rights reserved.
//


#import "STStructViewController.h"
#import "STStructModel.h"
#import <objc/runtime.h>
struct person {
    NSInteger  age;
    CGFloat  height;
} person1;//定义了一个 person1 的结构体变量， 也可以不写
struct  {
    NSInteger  price;
    CGFloat  height;
   
} fruit1;//定义了一个 fruit1 的结构体变量， 也可以不写,这样保证 只会有一个变量
typedef struct person CGPerson;//定义之后，原来结构体 person 不可以使用  CGPerson变成模板
typedef struct person *zhizhenPerson;
typedef struct{

    NSInteger carUseAge;
    NSInteger carPrice;
    CGPerson  carOwner;//结构体里面可以申明结构体类型
} carDefult1,carDefult2;
// 定义两个 模板 carDefult1 carDefult2 都可以来定义 这个结构体

static CGPerson CGPersonMake(NSInteger age,CGFloat height){

    CGPerson p;
    p.age = age;
    p.height = height;
    return p;
}
typedef struct objc_object *STid;
@interface STStructViewController ()
@property(nonatomic,strong)STStructModel            *currentModel;

@property(nonatomic,assign)int                      age;
@end

@implementation STStructViewController

- (void)viewDidLoad {
    [super viewDidLoad];
 
    CGPerson me = CGPersonMake(10, 100);
    carDefult1 a;
    a.carPrice = 10;
    a.carUseAge = 100;
    a.carOwner = CGPersonMake(10, 100);
    [STStructViewController superclass];
 

    STid i = 10;
 
    zhizhenPerson person = &me;
    CGPerson hahaperson  = *person;
    NSLog(@"%ld",hahaperson.age);
    
    CGPoint point = CGPointMake(10, 10);
    CGPoint *zhizhenPoint = &point ;
    zhizhenPoint->x = 99;
    
    Method  method = class_getMethodImplementation([self class], @selector(didReceiveMemoryWarning));

    //基本数据类型测试
    NSInteger myage = 10;
    NSInteger * _myage = &myage;
    myage = 199;
    NSInteger test = *_myage;
    NSLog(@"地址%p",_myage);
    NSLog(@"test = %ld",(long)test);
    
    //自定义类测试
    NSLog(@"self.currentModel --堆地址:%p",&self->_currentModel);
    STStructModel * model = [STStructModel new];
    model.name = @"hahha";
    model.weight = @"88";
    STStructModel * testModel = model;
    self.currentModel = model;
    self.age = 10;
    NSLog(@"model --堆地址:%p ,test --堆地址:%p,self.currentModel --堆地址:%p",model,testModel,self.currentModel);
    
    NSLog(@"model --栈地址:%p ,test --栈地址:%p,self.currentModel --堆地址:%p",&model,&testModel,&self->_currentModel);

    

    

    
    void (*swip)() = class_getMethodImplementation([self class], @selector(swipA:andB:));
    swip();
    
    //指针经典案列
    
    int p = 10;
    int *pp = &p;
    int **ppp = &pp;
    p = 100;
    
    NSLog(@"\n p = %d    pp = %d  ppp = %d",p,*pp,**ppp);
    
    
    // value 地址
    NSLog(@"value ");
    NSLog(@"\n p = %p    pp = %p  ppp = %p",p,pp,ppp);
    // value 地址
    NSLog(@"dizhi ");
    NSLog(@"\n p = %p    pp = %p  ppp = %p",&p,&pp,&ppp);
    
    // Do any additional setup after loading the view.
}
//普通赋值
- (void)swipA:(int) a andB:(int) b{

    int temp = a;
    a = b;
    b = temp;


}
//指针赋值
- (void)zhizhenSwipA:(int*) a andB:(int*) b{
    
    int *temp = a;
    a = b;
    b = temp;
    NSLog(@"a = %d,b = %d",a,b);
    
}
//引用
- (void)yinyongSwipA:(int)a andB:(int)b{
    
    int *temp = a;
    a = b;
    b = temp;
    NSLog(@"a = %d,b = %d",a,b);
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    NSLog(@"被调用");
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
